package com.heima.minio;

import com.heima.file.service.FileStorageService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @projectName: heima-leadnews
 * @package: com.heima.minio
 * @className: MinIOApplicationDemo
 * @author: Eric
 * @description: TODO
 * @date: 2023/11/5 17:42
 * @version: 1.0
 */
@SpringBootTest
public class MinIOApplicationDemo {

    @Autowired
    private FileStorageService fileStorageService;

    @Test
    public void test() throws Exception{
        // 创建minio客户端
        MinioClient minioClient = MinioClient.builder()   // builder()构造,本质new MinioClient
                .credentials("minio", "minio123") // 账号和密码
                .endpoint("http://192.168.200.130:9000")             // 地址
                .build();

        // 读取本地文件
        FileInputStream fileInputStream = new FileInputStream("F:\\Git\\leadnews\\heima-leadnews" +
                "\\heima-leadnews-test\\Freemarker-demo\\src\\main\\resources\\index.html");

        // 创建上传对象
        PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                .bucket("leadnews")        // 桶
                .object("index.html")      // 文件上传后的名字
                .contentType("text/html")  // 文件的类型
                // stream的三个参数(文件,文件的大小,分片)  partSize分片的大小  -1:代表不分配
                .stream(fileInputStream, fileInputStream.available(), -1)
                .build();
        // 上传
        minioClient.putObject(putObjectArgs);
    }

    @Test
    public void test1() throws FileNotFoundException {
        FileInputStream fileInputStream = new FileInputStream("F:\\Git\\leadnews\\heima-leadnews" +
                "\\heima-leadnews-test\\Freemarker-demo\\src\\main\\resources\\index.html");
        fileStorageService.uploadHtmlFile("", "index1.html", fileInputStream);
    }
}
